package cn.edu.scau.cmi.mengzhuorong.hibernate;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import java.util.List;
import cn.edu.scau.cmi.mengzhuorong.hibernate.dao.StudentDAO;
import cn.edu.scau.cmi.mengzhuorong.hibernate.dao.TeacherDAO;
import cn.edu.scau.cmi.mengzhuorong.hibernate.domain.Student;
import cn.edu.scau.cmi.mengzhuorong.hibernate.domain.Teacher;

public class UI {
	GridPane pane = new GridPane();

	public UI() {
		pane.setHgap(30);
		pane.setVgap(10);
		pane.setPadding(new Insets(10, 30, 20, 30));
		pane.setMaxHeight(760);
		pane.add(update(), 1, 0); // 个人信息，0行0列
		pane.add(crud_ui(), 0, 1);
		pane.add(select_ui(), 1, 1);
		pane.add(new Label("【学生表】"), 0, 2);
		pane.add(showStuList(), 0, 3);
		pane.add(new Label("【教师表】"), 1, 2);
		pane.add(showTeaList(), 1, 3);
	}

	public GridPane crud_ui() {
		GridPane ui1 = new GridPane();
		ui1.add(breakline(), 0, 1);
		ui1.add(Student(), 0, 2); // 添加/删除学生
		ui1.add(breakline(), 0, 3);
		ui1.add(Teacher(), 0, 4); // 添加老师
		ui1.add(breakline(), 0, 5);
		ui1.add(breakline(), 0, 7);
		return ui1;
	}

	public GridPane select_ui() {
		GridPane ui2 = new GridPane();
		ui2.add(breakline(), 0, 1);
		ui2.add(opStudent(), 0, 2); // 学生选择
		ui2.add(breakline(), 0, 3);
		ui2.add(opTeacher(), 0, 4); // 老师选择
		ui2.add(breakline(), 0, 5);

		ui2.add(breakline(), 0, 7);
		return ui2;
	}

	public GridPane Student() {
		GridPane addStuPane = new GridPane();
		addStuPane.setHgap(10);
		addStuPane.setVgap(10);
		
		Label Lname = new Label("学生姓名：");
		TextField Tname = new TextField();
		Button bt1 = new Button("添加学生");
		addStuPane.add(Lname, 0, 0);
		addStuPane.add(Tname, 1, 0);
		addStuPane.add(bt1, 2, 0);

		Label Lid = new Label("学生学号：");
		TextField Tid = new TextField();
		Button bt2 = new Button("删除学生");
		addStuPane.add(Lid, 0, 1);
		addStuPane.add(Tid, 1, 1);
		addStuPane.add(bt2, 2, 1);
		
		Student student = new Student();
		
		bt1.setOnAction(e -> {
			student.setName(Tid.getText());
			student.setId((long) Long.parseLong(Tname.getText()));
			HibernateControl.addStudent(student);
			handle("添加学生信息");
		});
		
		bt2.setOnAction(e -> {
			student.setName(Tname.getText());
			student.setId((long) Long.parseLong(Tid.getText()));
			HibernateControl.deleteStudent(student);
			handle("删除学生信息");
		});

		Button bt3 = new Button("修改学生信息");
		addStuPane.add(bt3, 1, 2);
		bt3.setOnAction(e -> {
			String name = Tname.getText();
			long id = (long) Long.parseLong(Tid.getText());
			Student stu = new StudentDAO().findById(id);
			stu.setName(name);
			HibernateControl.updateStudent(stu);
			handle("修改学生信息");
		});

		return addStuPane;
	}

	// 1.2-老师增删查改面板
	public GridPane Teacher() {
		GridPane addTeaPane = new GridPane();
		addTeaPane.setHgap(10);
		addTeaPane.setVgap(10);

		Label Lname = new Label("教师姓名：");
		TextField Tname = new TextField();
		Button bt1 = new Button("添加教师");
		addTeaPane.add(Lname, 0, 0);
		addTeaPane.add(Tname, 1, 0);
		addTeaPane.add(bt1, 2, 0);
		
		Label Lid = new Label("教师编号：");
		TextField Tid = new TextField();
		Button bt2 = new Button("删除教师");
		addTeaPane.add(Lid, 0, 1);
		addTeaPane.add(Tid, 1, 1);
		addTeaPane.add(bt2, 2, 1);
		
		bt1.setOnAction(e -> {
			Teacher teacher = new Teacher();
			teacher.setName(Tid.getText());
			teacher.setId((long) Long.parseLong(Tname.getText()));
			HibernateControl.addTeacher(teacher);
			handle("添加教师信息");
		});


		bt2.setOnAction(e -> {
			Teacher teacher = new Teacher();
			teacher.setName(Tname.getText());
			teacher.setId((long) Long.parseLong(Tid.getText()));
			HibernateControl.deleteTeacher(teacher);
			handle("删除教师信息");
		});

		Button bt3 = new Button("修改教师信息");
		addTeaPane.add(bt3, 1, 2);
		bt3.setOnAction(e -> {
			String name = Tname.getText();
			long id = (long) Long.parseLong(Tid.getText());
			Teacher tea = new TeacherDAO().findById(id);
			tea.setName(name);
			HibernateControl.updateTeacher(tea);
			handle("修改教师信息");
		});
		return addTeaPane;
	}

	// 2.1-学生选择修改面板
	public GridPane opStudent() {
		GridPane opStuPane = new GridPane();
		opStuPane.setHgap(10);
		opStuPane.setVgap(10);

		Label Lid = new Label("学生学号：");
		TextField Tid_s = new TextField();
		opStuPane.add(Lid, 0, 0);
		opStuPane.add(Tid_s, 1, 0);

		Label Lid2 = new Label("导师编号：");
		TextField Tid_t = new TextField();
		opStuPane.add(Lid2, 0, 1);
		opStuPane.add(Tid_t, 1, 1);

		HBox bt = new HBox();
		bt.setSpacing(10);
		Button bt1 = new Button("修改选择导师");
		Button bt2 = new Button("删除导师");
		bt.getChildren().addAll(bt1, bt2);
		opStuPane.add(bt, 1, 2);

		bt1.setOnAction(e -> {
			long sid = (long) Long.parseLong(Tid_s.getText());
			long tid = (long) Long.parseLong(Tid_t.getText());
			Student stu = new StudentDAO().findById(sid);
			stu.setTeacher(new TeacherDAO().findById(tid));
			HibernateControl.updateStudent(stu);
			handle("修改选择导师");
		});
		bt2.setOnAction(e -> {
			long sid = (long) Long.parseLong(Tid_s.getText());
			Student stu = new StudentDAO().findById(sid);
			stu.setTeacher(null);
			HibernateControl.updateStudent(stu);
			handle("删除导师");
		});

		return opStuPane;
	}

	// 2.2-导师选择修改面板
	public GridPane opTeacher() {
		GridPane opTPane = new GridPane();
		opTPane.setHgap(10);
		opTPane.setVgap(10);

		Label Lid = new Label("导师编号：");
		TextField Tid_t = new TextField();
		opTPane.add(Lid, 0, 0);
		opTPane.add(Tid_t, 1, 0);

		Label Lid2 = new Label("学生学号：");
		TextField Tid_s = new TextField();
		opTPane.add(Lid2, 0, 1);
		opTPane.add(Tid_s, 1, 1);

		HBox bt = new HBox();
		bt.setSpacing(10);
		Button bt1 = new Button("修改选择学生");
		Button bt2 = new Button("删除学生");
		bt.getChildren().addAll(bt1, bt2);
		opTPane.add(bt, 1, 2);

		bt1.setOnAction(e -> {
			long sid = (long) Long.parseLong(Tid_s.getText());
			long tid = (long) Long.parseLong(Tid_t.getText());
			Student stu = new StudentDAO().findById(sid);
			stu.setTeacher(new TeacherDAO().findById(tid));
			HibernateControl.updateStudent(stu);
			handle("修改学生信息");
		});
		bt2.setOnAction(e -> {
			long sid = (long) Long.parseLong(Tid_s.getText());
			Student stu = new StudentDAO().findById(sid);
			stu.setTeacher(null);
			HibernateControl.updateStudent(stu);
			handle("删除指导学生");
		});

		return opTPane;
	}


	// 提示操作成功消息（文本框）
	public VBox getInfo(String infoStr) {
		TextArea Info = new TextArea();
		// Info.setMinSize(200, 300);
		Info.setText(infoStr);
		VBox InfoPane = new VBox();
		InfoPane.setMaxWidth(400);
		InfoPane.setMaxHeight(100);
		InfoPane.setPadding(new Insets(10, 20, 10, 20));
		InfoPane.getChildren().add(Info);
		return InfoPane;
	}

	// 水平分界线
	public VBox breakline() {
		VBox xian = new VBox();
		xian.setPadding(new Insets(10, 20, 10, 20));
		xian.getChildren().add(new Separator());
		return xian;
	}


	public TableView showStuList() {
		TableView<Student> table = new TableView();
		table.setMaxWidth(370);
		StudentDAO studentDAO = new StudentDAO();
		List<Student> students = studentDAO.findAll();
		// table.setEditable(true); //可编辑
		ObservableList<Student> data = FXCollections.observableArrayList(students);

		TableColumn StuIdCol = new TableColumn("Student_Id");
		StuIdCol.setMinWidth(100);
		StuIdCol.setCellValueFactory(new PropertyValueFactory<>("id"));

		TableColumn StuNameCol = new TableColumn("Student_Name");
		StuNameCol.setMinWidth(150);
		StuNameCol.setCellValueFactory(new PropertyValueFactory<>("name"));

		TableColumn TutornameCol = new TableColumn("Tutor");
		TutornameCol.setMinWidth(100);
		TutornameCol.setCellValueFactory(new PropertyValueFactory<>("teacher"));

		table.setItems(data);
		table.getColumns().addAll(StuIdCol, StuNameCol, TutornameCol);

		StuIdCol.setSortType(TableColumn.SortType.DESCENDING); //降序
		StuNameCol.setSortable(false); //不可排序

		return table;
	}


	public TableView showTeaList() {
		TableView<Teacher> table = new TableView();
		table.setMaxWidth(270);
		TeacherDAO teacherDAO = new TeacherDAO();
		List<Teacher> teachers = teacherDAO.findAll();
		ObservableList<Teacher> data = FXCollections.observableArrayList(teachers);

		TableColumn TeaIdCol = new TableColumn("Teacher_Id");
		TeaIdCol.setMinWidth(100);
		TeaIdCol.setCellValueFactory(new PropertyValueFactory<>("id"));

		TableColumn TeaNameCol = new TableColumn("Teacher_Name");
		TeaNameCol.setMinWidth(150);
		TeaNameCol.setCellValueFactory(new PropertyValueFactory<>("name"));

		table.setItems(data);
		table.getColumns().addAll(TeaIdCol, TeaNameCol);

		TeaIdCol.setSortType(TableColumn.SortType.DESCENDING); //降序
		TeaNameCol.setSortable(false); //不可排序

		return table;
	}

	// 0-2 ���°�ť
	public GridPane update() {
		GridPane up = new GridPane();
		up.setPadding(new Insets(0, 0, 0, 70));
		up.setHgap(30);
		Button stu = new Button("更新学生表");
		Button tea = new Button("更新教师表");
		up.add(stu, 0, 1);
		up.add(tea, 1, 1);
		stu.setOnAction(e -> {
			pane.add(showStuList(), 0, 3);
			handle("更新学生表");
		});
		tea.setOnAction(e -> {
			pane.add(showTeaList(), 1, 3);
			handle("更新教师表");
		});
		return up;
	}

	public void handle(String str) {
		Alert information = new Alert(Alert.AlertType.INFORMATION, "操作完毕");
		information.setTitle("������ʾ"); //设置标题，不设置默认标题为本地语言的information
		information.setHeaderText(str); //设置头标题，默认标题为本地语言的informatio
		information.showAndWait(); //显示弹窗，同时后续代码等挂起
	}

	// 获取主面板，传回client
	public GridPane getPane() {
		return pane;
	}
}
